//
//  UIView+Extension.m
//  JWBrowser
//
//  Created by byAlex on 14-11-6.
//  Copyright (c) 2014年 JW. All rights reserved.
//

#import "UIView+Extension.h"
#define BlurViewTag 100000

@implementation UIView (Extension)

- (void)enableBlurEffect
{
    self.backgroundColor = [UIColor clearColor];
    
    UIView *blurView = [self viewWithTag:BlurViewTag];
    if (!blurView) {
        CGRect frame = CGRectZero;
        if ([self isKindOfClass:[UIScrollView class]]) {
            UIScrollView *scrollView = (UIScrollView *)self;
            frame = self.bounds;
            frame.size.width = scrollView.contentSize.width;
        }else
        {
            frame = self.bounds;
        }
        
        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:frame];
        toolbar.barStyle = UIBarStyleDefault;
        toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        toolbar.tag = BlurViewTag;
        
        [self insertSubview:toolbar atIndex:0];
    }
}

- (void)disableBlurEffect
{
    UIView *blurView = [self viewWithTag:BlurViewTag];
    if (blurView) {
        [blurView removeFromSuperview];
    }
}

- (void)setBackgroundImageWithImageName:(NSString *)imageName
{
    UIImageView *imageView = (UIImageView *)[self viewWithTag:10088888];
    if (!imageView) {
        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        bgImageView.tag = 10088888;
        bgImageView.image = [UIImage imageNamed:imageName];
        bgImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self insertSubview:bgImageView atIndex:0];
    }else
    {
        imageView.image = [UIImage imageNamed:imageName];
    }
}
@end
